package com.yazi.mxz.server.controller;

import java.util.List;

import javax.annotation.Resource;

import com.fasterxml.jackson.annotation.JsonView;
import com.yazi.mxz.common.entity.beans.Address;
import com.yazi.mxz.common.entity.beans.Member;
import com.yazi.mxz.common.service.AddressService;
import com.yazi.mxz.server.response.AddressListResponse;
import com.yazi.mxz.server.response.AddressResponse;
import com.yazi.mxz.server.response.StatusResponse;
import com.yazi.mxz.server.response.view.BaseView;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller("addressController")
@RequestMapping("/address")
public class AddressController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(AddressController.class);
	
	@Resource
	AddressService addressService;
	
	@RequestMapping("/list")
	@ResponseBody
	@JsonView(BaseView.class)
	public AddressListResponse list(){
		log.debug("客户端请求 ->　获取收货地址列表");
		AddressListResponse res = new AddressListResponse();
		try {
			Address address = new Address();
			address.setMember(getLoginMember());
			
			List<Address> addressList = addressService.search(address, null, null);
			res.setSuccessData(addressList);
			res.setOkMessage("ok");
			
		} catch (Exception e) {
			log.error("客户端获取收货地址列表时异常:", e);
			res.setErrorMessage(e.getMessage());
		}
		
		return res;
	}
	
	@RequestMapping("/removeDo")
	@ResponseBody
	public StatusResponse removeDo(@RequestBody Long addressId){
		Long memberId = getLoginMember().getMemberId();
		log.debug("客户端请求 ->　删除收货地址: memberId={},addressId={}", memberId, addressId);
		StatusResponse res = new StatusResponse();
		try {
			addressService.remove(memberId, addressId);
			res.setOkMessage("删除成功");
		} catch (Exception e) {
			log.error("客户端删除收货地址异常:", e);
			res.setErrorMessage("删除失败,请联系管理员");
		}
		return res;
	}
	
	@RequestMapping("/addDo")
	@ResponseBody
	@JsonView(BaseView.class)
	public AddressResponse addDo(@RequestBody Address address){
		Member loginMember = getLoginMember();
		log.debug("客户端请求 ->　添加收货地址: memberId={}", loginMember.getMemberId());
		AddressResponse res = new AddressResponse();
		try {
			address.setMember(loginMember);
			address = addressService.add(address);
			res.setSuccessData(address);
			res.setOkMessage("添加成功");
		} catch (Exception e) {
			log.error("客户端用户添加收货地址异常:", e);
			res.setErrorMessage("添加失败,请联系管理员");
		}
		return res;
	}
	
	@RequestMapping("/setDefaultDo")
	@ResponseBody
	public StatusResponse updateDefaultDo(@RequestBody Long addressId){
		Long memberId = getLoginMember().getMemberId();
		log.debug("客户端请求 ->　设置默认收货地址: memberId={},addressId={}", memberId, addressId);
		StatusResponse res = new StatusResponse();
		try {
			addressService.updateDefault(memberId, addressId);
			res.setOkMessage("设置成功");
		} catch (Exception e) {
			log.error("客户端设置默认收货地址异常:", e);
			res.setErrorMessage("设置失败,请联系管理员");
		}
		return res;
	}
}
